#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "prober.h"
#include <QTimer>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    limit_rssi(-100)
{
    ui->setupUi(this);

    Prober *m_prober = new Prober(this);
    m_prober->startListen();

    qRegisterMetaType<ProberListStruct>("ProberListStruct");
    connect(m_prober,SIGNAL(sendProberList(const ProberListStruct &)),this,SLOT(dealProberList(const ProberListStruct &)));

   limit_rssi = ui->horizontalSlider->value();

   Profiles::instance()->setDefault(QStringLiteral("MAC"),QStringLiteral("f483cded13e1"),QStringLiteral("测试"));

   ui->horizontalSlider->setValue(Profiles::instance()->value(QStringLiteral("RSSI"),QStringLiteral("LIMITRSSI"),-99).toInt());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateCaption()
{

}
/*-------------------------------------------
*
* 处理MAC数据
*
---------------------------------------------*/
void MainWindow::dealProberList(const ProberListStruct &m_prolst)
{
    if(ui->probeBox->isChecked())
    {
        ui->tableWidget->clear();
        ui->rssilabel->setText(QStringLiteral("RSSI：%1").arg(ui->horizontalSlider->value()));
        limit_rssi = ui->horizontalSlider->value();

        ProberListStruct proberslst;
        proberslst.device_sn = m_prolst.device_sn;
        foreach (ProberStruct m_prober, m_prolst.probers)
        {
            if(m_prober.mac.toUpper() == QStringLiteral("64:CC:2E:84:0C:52"))
            {
                qDebug()<<"ddddddddddddd" << m_prober.mac.toUpper();
            }

            QString m_mac = m_prober.mac;
            m_mac.replace(":","");
            m_prober.alias_name = Profiles::instance()->value(QStringLiteral("MAC"),m_mac.toLower()).toString();
            if(m_prober.rssi.toInt() >= limit_rssi)
            {
                proberslst.probers.append(m_prober);
            }
        }

        QStringList headerLabels;
        headerLabels.append(QStringLiteral("序号"));
        headerLabels.append(QStringLiteral("机器号"));
        headerLabels.append(QStringLiteral("识别号"));
        headerLabels.append(QStringLiteral("MAC地址"));
        headerLabels.append(QStringLiteral("RSSI"));
        headerLabels.append(QStringLiteral("通道"));
        headerLabels.append(QStringLiteral("次数"));
        ui->tableWidget->setHorizontalHeaderLabels(headerLabels);

        ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
        ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->setColumnWidth(0,60);
        ui->tableWidget->setColumnWidth(3,133);

        ui->tableWidget->setRowCount(proberslst.probers.count());
        int index = 0;
        foreach (ProberStruct prober, proberslst.probers)
        {
            ui->tableWidget->setItem(index,0,new QTableWidgetItem(QString("%1").arg(index+1)));
            ui->tableWidget->setItem(index,1,new QTableWidgetItem(QString("%1").arg(proberslst.device_sn)));
            ui->tableWidget->setItem(index,2,new QTableWidgetItem(QString("%1").arg(prober.alias_name)));
            ui->tableWidget->setItem(index,3,new QTableWidgetItem(QString("%1").arg(prober.mac)));
            ui->tableWidget->setItem(index,4,new QTableWidgetItem(QString("%1").arg(prober.rssi)));
            ui->tableWidget->setItem(index,5,new QTableWidgetItem(QString("%1").arg(prober.chenal)));
            ui->tableWidget->setItem(index,6,new QTableWidgetItem(QString("%1").arg(0)));
            index++;
        }

        ui->totallabel->setText(QStringLiteral("卡片个数：%1").arg(proberslst.probers.count()));
    }
}
/*-------------------------------------------
*
* 清除
*
---------------------------------------------*/
void MainWindow::on_clearButton_clicked()
{
    ui->tableWidget->clear();
    QStringList headerLabels;
    headerLabels.append(QStringLiteral("序号"));
    headerLabels.append(QStringLiteral("机器号"));
    headerLabels.append(QStringLiteral("识别号"));
    headerLabels.append(QStringLiteral("MAC地址"));
    headerLabels.append(QStringLiteral("RSSI"));
    headerLabels.append(QStringLiteral("通道"));
    headerLabels.append(QStringLiteral("次数"));
    ui->tableWidget->setHorizontalHeaderLabels(headerLabels);
    ui->tableWidget->setRowCount(0);
}
/*-------------------------------------------
*
* rssi限制
*
---------------------------------------------*/
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    ui->tableWidget->clear();
    QStringList headerLabels;
    headerLabels.append(QStringLiteral("序号"));
    headerLabels.append(QStringLiteral("机器号"));
    headerLabels.append(QStringLiteral("识别号"));
    headerLabels.append(QStringLiteral("MAC地址"));
    headerLabels.append(QStringLiteral("RSSI"));
    headerLabels.append(QStringLiteral("通道"));
    headerLabels.append(QStringLiteral("次数"));
    ui->tableWidget->setHorizontalHeaderLabels(headerLabels);
    ui->tableWidget->setRowCount(0);

    ui->rssilabel->setText(QStringLiteral("RSSI：%1").arg(ui->horizontalSlider->value()));
    ui->totallabel->setText(QStringLiteral("卡片个数：%1").arg(0));

    Profiles::instance()->setValue(QStringLiteral("RSSI"),QStringLiteral("LIMITRSSI"),ui->horizontalSlider->value());
    Q_UNUSED(value)
}
